﻿using Cms.Core.Infrastructure.Entity;
using Cms.Core.Infrastructure.ExpressionUtil;
using Cms.Core.Infrastructure.Extension;
using Cms.Core.Infrastructure.Ui;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Dynamic.Core;
using Cms.Core.Infrastructure.Filter;
using Cms.Core.Infrastructure.Common;

namespace Cms.Core.Infrastructure.Extension
{
    public static class QueryableExtensions
    {

        /// <summary>
        /// 扩展Between 操作符
        /// </summary>
        /// <typeparam name="TSource">动态实体</typeparam>
        /// <typeparam name="TKey">键</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="low">低</param>
        /// <param name="high">高</param>
        /// <returns></returns>
        public static IQueryable<TSource> Between<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector,
            TKey low, TKey high) where TKey : IComparable<TKey>
        {

            Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(source.GenerateBetween(keySelector, low, high), keySelector.Parameters);
            return source.Where(lambda);
        }


        /// <summary>
        /// 扩展NotBetween操作符
        /// </summary>
        /// <typeparam name="TSource">动态实体</typeparam>
        /// <typeparam name="TKey">键</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="low">低</param>
        /// <param name="high">高</param>
        /// <returns></returns>
        public static IQueryable<TSource> NotBetween<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector,
            TKey low, TKey high) where TKey : IComparable<TKey>
        {
            //取反
            var not = Expression.Not(source.GenerateBetween(keySelector, low, high));
            Expression<Func<TSource, bool>> lambda = Expression.Lambda<Func<TSource, bool>>(not, keySelector.Parameters);
            return source.Where(lambda);
        }
        /// <summary>
        /// 生成Between操作符
        /// </summary>
        /// <typeparam name="TSource">动态实体</typeparam>
        /// <typeparam name="TKey">键</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="keySelector">键选择器</param>
        /// <param name="low">低</param>
        /// <param name="high">高</param>
        /// <returns></returns>

        private static Expression GenerateBetween<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector,
            TKey low, TKey high) where TKey : IComparable<TKey>
        {
            source.NotNull(nameof(source));
            keySelector.NotNull(nameof(keySelector));
            low.NotNull(nameof(low));
            high.NotNull(nameof(high));
            Expression key = Expression.Invoke(keySelector, keySelector.Parameters);
            Expression lowerBound = Expression.GreaterThanOrEqual(key, Expression.Constant(low)); //18
            Expression upperBound = Expression.LessThanOrEqual(key, Expression.Constant(high)); //21
            Expression and = Expression.AndAlso(lowerBound, upperBound);
            return and;
        }


        /// <summary>
        /// 开始日期、截止日期。
        /// </summary>
        /// <typeparam name="T">动态对象</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="getter"></param>
        /// <param name="from">来自</param>
        /// <param name="to">到</param>
        /// <returns></returns>
        public static IQueryable<T> DateRange<T>(this IQueryable<T> source, Expression<Func<T, DateTime>> getter, DateTime from, DateTime to)
        {
            source.NotNull(nameof(source));
            getter.NotNull(nameof(getter));

            Expression body = getter.Body;
            var greaterExp = Expression.GreaterThanOrEqual(body, Expression.Constant(from));
            var lessExp = Expression.LessThanOrEqual(body, Expression.Constant(to));
            var and = Expression.And(greaterExp, lessExp);
            var lambda = Expression.Lambda<Func<T, bool>>(and, getter.Parameters);
            return source.Where(lambda);
        }


        /// <summary>
        ///  从指定<see cref="IQueryable{TSource}"/>集合中查询条件过滤
        /// </summary>
        /// <typeparam name="TSource">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="condition">条件过滤</param>
        /// <returns>返回已过滤的查询集合</returns>
        public static IQueryable<TSource> WhereIf<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, bool condition)
        {
            source.NotNull(nameof(source));
            predicate.NotNull(nameof(predicate));

            return condition ? source.Where(predicate) : source;
        }
        /// <summary>
        /// 转成dto
        /// </summary>
        /// <typeparam name="TSource">数据源</typeparam>
        /// <typeparam name="TDestination">目标</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="membersToExpand">过滤</param>
        /// <returns></returns>
        public static IQueryable<TDestination> ToDto<TSource, TDestination>(this IQueryable<TSource> source)

        {
            if (source.IsNull())
            {
                return default(IQueryable<TDestination>);
            }
            return new ProjectionExpression<TSource>(source).To<TDestination>();
        }

        /// <summary>
        /// 转成dto
        /// </summary>
        /// <typeparam name="TSource">数据源</typeparam>
        /// <typeparam name="TDestination">目标</typeparam>
        /// <param name="source">数据源</param>
        /// <param name="membersToExpand">过滤</param>
        /// <returns></returns>
        public static IQueryable<TDestination> ToDto<TSource, TDestination>(this IQueryable<TSource> source, Expression<Func<TDestination, bool>> predicate)

        {
            source.NotNull(nameof(source));
            predicate.NotNull(nameof(predicate));
            return source.ToDto<TSource, TDestination>().Where(predicate);
        }


        /// <summary>
        ///从指定<see cref="IQueryable{TResult}"/>集合中查询指定数据筛选的分页信息
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <typeparam name="TResult">返回动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <param name="selector">数据筛选表达式</param>
        /// <returns></returns>

        public static async Task<PageResult<TResult>> ToPageAsync<TEntity, TResult>(this IQueryable<TEntity> source, Expression<Func<TEntity, bool>> predicate, PageRequestData pageRequest, Expression<Func<TEntity, TResult>> selector)
        {
            pageRequest.NotNull(nameof(pageRequest));
            try
            {


                var quereDataAsync = await source.WhereAsync(pageRequest.PageIndex, pageRequest.PageSize, predicate, pageRequest.OrderBy);
                var list = await quereDataAsync.data.Select(selector).ToListAsync();
                return new PageResult<TResult>(list, quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.Run(() => new PageResult<TResult>(new List<TResult>(), 0,e.Message, false));
            }
        }


        //public static async Task<PageResult<TResult>> ToPageAsync<TEntity, TResult>(this IQueryable source, Expression<Func<TEntity, bool>> predicate, PageRequestData pageRequest)
        //{
        //    pageRequest.NotNull(nameof(pageRequest));
        //    try
        //    {


        //        var quereDataAsync = await source.WhereAsync(pageRequest.PageIndex, pageRequest.PageSize, predicate, pageRequest.OrderBy);
        //        var list = await quereDataAsync.data.ToListAsync();
        //        return new PageResult<TResult>(list, quereDataAsync.totalNumber);
        //    }
        //    catch (Exception e)
        //    {

        //        return await Task.Run(() => new PageResult<TResult>(new List<TResult>(), 0, e.Message, false));
        //    }
        //}




        /// <summary>
        ///从指定<see cref="IQueryable{TResult}"/>集合中查询指定数据筛选的分页信息
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns></returns>

        public static async Task<PageResult<TEntity>> ToPageAsync<TEntity>(this IQueryable<TEntity> source, Expression<Func<TEntity, bool>> predicate, PageRequestData pageRequest)
        {
            pageRequest.NotNull(nameof(pageRequest));
            try
            {

                var quereDataAsync = await source.WhereAsync(pageRequest.PageIndex, pageRequest.PageSize, predicate, pageRequest.OrderBy);
                var list = await quereDataAsync.data.ToListAsync<TEntity>();
                return new PageResult<TEntity>(list, quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.FromResult(new PageResult<TEntity>(new List<TEntity>(), 0,e.Message, false));
            }

        }

        /// <summary>
        /// 异步分页后转成Dto
        /// </summary>
        /// <typeparam name="TSource">动态实体类型</typeparam>
        /// <typeparam name="TDto">Dto实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <returns></returns>
        public static async Task<PageResult<TDto>> ToPageAsync<TSource, TDto>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, PageRequestData pageRequest)
        {

            pageRequest.NotNull(nameof(pageRequest));
            try
            {

                var quereDataAsync = await source.WhereAsync(pageRequest.PageIndex, pageRequest.PageSize, predicate, pageRequest.OrderBy);
                List<TDto> dtos = await quereDataAsync.data.ToDto<TSource, TDto>().ToListAsync();
                return new PageResult<TDto>(dtos, quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.Run(() => new PageResult<TDto>(new List<TDto>(), 0,e.Message, false));

            }

        }



        ///// <summary>
        ///// 异步分页后转成Dto
        ///// </summary>
        ///// <typeparam name="TSource">动态实体类型</typeparam>
        ///// <typeparam name="TDto">Dto实体类型</typeparam>
        ///// <param name="source">要查询的数据集</param>
        ///// <param name="pageRequest">分页请求参数</param>
        ///// <param name="predicate">查询条件谓语表达式</param>
        /////<param name="orderConditions">排序条件集合</param>
        ///// <returns></returns>
        //public static async Task<PageResult<TDto>> ToPageAsync<TSource, TDto>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate, PageRequestData pageRequest, OrderCondition[] orderConditions)
        //{

        //    pageRequest.NotNull(nameof(pageRequest));
        //    predicate.NotNull(nameof(predicate));
        //    orderConditions.NotNull(nameof(orderConditions));
        //    try
        //    {


        //        int total = await source.CountAsync(predicate);
        //        source = source.Where(predicate);
        //        IOrderedQueryable<TSource> orderSource = null;
        //        if (orderConditions == null || orderConditions.Length == 0)
        //        {
        //            orderSource = CollectionPropertySorter<TSource>.OrderBy(source, "Id", SortDirection.Ascending);
        //        }

        //        int count = 0;
                
        //        foreach (OrderCondition orderCondition in orderConditions)
        //        {
        //            orderSource = count == 0
        //                ? CollectionPropertySorter<TSource>.OrderBy(source, orderCondition.SortField, orderCondition.SortDirection)
        //                : CollectionPropertySorter<TSource>.ThenBy(orderSource, orderCondition.SortField, orderCondition.SortDirection);
        //            count++;
        //        }

        //        source = orderSource;
        //        source = source.IsNotNull() ? source.Skip(pageRequest.PageSize * (pageRequest.PageIndex - 1)).Take(pageRequest.PageSize) : Enumerable.Empty<TSource>().AsQueryable();
        //        var dtos = await source.ToDto<TSource, TDto>().ToListAsync();
        //        return new PageResult<TDto>(dtos, total, "查询成功");
        //    }
        //    catch (Exception e)
        //    {

        //        return await Task.Run(() => new PageResult<TDto>(new List<TDto>(), 0, e.Message, false));

        //    }

        //}

   

        /// <summary>
        /// 异步分页后转成Dto
        /// </summary>
        /// <typeparam name="TSource">动态实体类型</typeparam>
        /// <typeparam name="TDto">Dto实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns></returns>
        public static async Task<PageResult<TDto>> ToPageAsync<TSource, TDto>(this IQueryable<TSource> source, PageRequestData pageRequest)
        {
            pageRequest.NotNull(nameof(pageRequest));
            try
            {

                var quereDataAsync = await source.WhereAsync(pageRequest.PageIndex, pageRequest.PageSize, null, pageRequest.OrderBy);
                List<TDto> dtos = await quereDataAsync.data.ToDto<TSource, TDto>().ToListAsync();
                return new PageResult<TDto>(dtos, quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.Run(() => new PageResult<TDto>(new List<TDto>(), 0,e.Message, false));

            }

        }

        /// <summary>
        /// 异步分页后转成动态类型
        /// </summary>
        /// <typeparam name="TSource">动态实体类型</typeparam>
        /// <param name="keySelector">过滤</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <returns>返回分页的数据</returns>
        public static async Task<PageResult<dynamic>> ToDynamincPageAsync<TSource>(this IQueryable<TSource> source,string keySelector,Expression<Func<TSource, bool>>  predicate, PageRequestData pageRequest)
        {
            
            pageRequest.NotNull(nameof(pageRequest));
            keySelector.NotNull(nameof(keySelector));
            //predicate.NotNull(nameof(predicate));
            try
            {

                var quereDataAsync = await source.WhereAsync(pageRequest.PageIndex, pageRequest.PageSize, predicate, pageRequest.OrderBy);
                var datas = await quereDataAsync.data.ToDynamincListAsync(keySelector);
                return new PageResult<dynamic>(datas.ToList(), quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.Run(() => new PageResult<dynamic>(new List<dynamic>(), 0,e.Message,false));

            }

        }


        /// <summary>
        ///  从指定<see cref="IQueryable{TSource}"/>集合中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TSource">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="orderBy">排序    </param>
        /// <returns></returns>

        public static async Task<(IQueryable<TSource> data, int totalNumber)> WhereAsync<TSource>(this IQueryable<TSource> source, int pageIndex,
            int pageSize, Expression<Func<TSource, bool>> predicate, string orderBy)
        {

            source.NotNull(nameof(source));

            //predicate.NotNull(nameof(predicate));
            //orderBy.NotNullOrEmpty(nameof(orderBy));
            try
            {
                int total = !predicate.IsNull() ? await source.CountAsync(predicate) : await source.CountAsync();
                if (!predicate.IsNull())
                {
                    source = source.Where(predicate);
                }
                //deac
                source = orderBy.IsNullOrEmpty() ? source.OrderBy("Id") : source.OrderBy(orderBy);
               
                return (!source.IsNull() ? source.Skip(pageSize * (pageIndex - 1)).Take(pageSize) : Enumerable.Empty<TSource>().AsQueryable(), total);
            }
            catch (Exception ex)
            {

                throw ex;
            }
         

        }

        private static async Task<(IQueryable<TSource> data, int totalNumber)> WhereAsync<TSource>(this IQueryable<TSource> source, int pageIndex,
            int pageSize, Expression<Func<TSource, bool>> predicate, OrderCondition[] orderConditions)
        {
     
            try
            {

                int total = await source.CountAsync(predicate);
                source = source.Where(predicate);
                IOrderedQueryable<TSource> orderSource = null;
                if (orderConditions == null || orderConditions.Length == 0)
                {
                    orderSource = CollectionPropertySorter<TSource>.OrderBy(source, "Id", SortDirection.Ascending);
                }

                int count = 0;

                foreach (OrderCondition orderCondition in orderConditions)
                {
                    orderSource = count == 0
                        ? CollectionPropertySorter<TSource>.OrderBy(source, orderCondition.SortField, orderCondition.SortDirection)
                        : CollectionPropertySorter<TSource>.ThenBy(orderSource, orderCondition.SortField, orderCondition.SortDirection);
                    count++;
                }

                source = orderSource;
            
                return (!source.IsNull() ? source.Skip(pageSize * (pageIndex - 1)).Take(pageSize) : Enumerable.Empty<TSource>().AsQueryable(), total);

            }
            catch (Exception ex)
            {
                throw ex;

            }
        }


        /// <summary>
        ///从指定<see cref="IQueryable{TResult}"/>集合中查询指定数据筛选的分页信息
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <typeparam name="TResult">返回动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <param name="selector">数据筛选表达式</param>
        /// <returns></returns>

        public static async Task<PageResult<TResult>> ToPageAsync<TEntity, TResult>(this IQueryable<TEntity> source, Expression<Func<TEntity, bool>> predicate, PageInputDto pagingInputDto, Expression<Func<TEntity, TResult>> selector)
        {
            pagingInputDto.NotNull(nameof(pagingInputDto));
            try
            {


                var quereDataAsync = await source.WhereAsync(pagingInputDto.PageIndex, pagingInputDto.PageSize, predicate, pagingInputDto.OrderConditions);
                var list = await quereDataAsync.data.Select(selector).ToListAsync();
                return new PageResult<TResult>(list, quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.Run(() => new PageResult<TResult>(new List<TResult>(), 0, e.Message, false));
            }
        }

        /// <summary>
        ///从指定<see cref="IQueryable{TResult}"/>集合中查询指定数据筛选的分页信息
        /// </summary>
        /// <typeparam name="TEntity">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="pageRequest">分页请求参数</param>
        /// <param name="selector">数据筛选表达式</param>
        /// <returns></returns>

        public static async Task<PageResult<TEntity>> ToPageAsync<TEntity>(this IQueryable<TEntity> source, Expression<Func<TEntity, bool>> predicate, PageInputDto pagingInputDto)
        {
            pagingInputDto.NotNull(nameof(pagingInputDto));
            try
            {


                var quereDataAsync = await source.WhereAsync(pagingInputDto.PageIndex, pagingInputDto.PageSize, predicate, pagingInputDto.OrderConditions);
                var list = await quereDataAsync.data.ToListAsync();
                return new PageResult<TEntity>(list, quereDataAsync.totalNumber);
            }
            catch (Exception e)
            {

                return await Task.Run(() => new PageResult<TEntity>(new List<TEntity>(), 0, e.Message, false));
            }
        }


        /// <summary>
        ///  从指定<see cref="IQueryable{TSource}"/>集合中查询指定分页条件的子数据集
        /// </summary>
        /// <typeparam name="TSource">动态实体类型</typeparam>
        /// <param name="source">要查询的数据集</param>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        /// <param name="predicate">查询条件谓语表达式</param>
        /// <param name="orderBy">排序    </param>
        /// <returns></returns>

        public static (IQueryable<TSource> data, int totalNumber) Where<TSource>(this IQueryable<TSource> source, int pageIndex, int pageSize, Expression<Func<TSource, bool>> predicate, string orderBy)
        {

            source.NotNull(nameof(source));
            predicate.NotNull(nameof(predicate));

            int total = source.Count(predicate);
            source = source.Where(predicate);
            source = orderBy.IsNullOrEmpty() ? source.OrderBy("Id deac") : source.OrderBy(orderBy);

            return (!source.IsNull() ? source.Skip(pageSize * (pageIndex - 1)).Take(pageSize) : Enumerable.Empty<TSource>().AsQueryable(), total);

        }

        /// <summary>
        /// 转成动态集合
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="keySelector">要过滤选择器</param>
        /// <returns></returns>
        public static IEnumerable<dynamic> ToDynamincList(this IQueryable source, string keySelector)
        {

            source.NotNull(nameof(source));
            keySelector.NotNull(nameof(keySelector));
            yield return ((IQueryable<dynamic>)source.Select(keySelector)).ToList();
        }

        /// <summary>
        /// 异步转成动态集合
        /// </summary>
        /// <param name="source">数据源</param>
        /// <param name="keySelector">要过滤选择器</param>
        /// <returns></returns>
        public static async Task<IEnumerable<dynamic>> ToDynamincListAsync(this IQueryable source, string keySelector)
        {

            source.NotNull(nameof(source));
            keySelector.NotNull(nameof(keySelector));
            return await ((IQueryable<dynamic>)source.Select(keySelector)).ToDynamicListAsync();
        }


        #region 排序
        /// <summary>
        /// 把<see cref="IQueryable{T}"/>集合按指定字段与排序方式进行排序
        /// </summary>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <typeparam name="T">动态类型</typeparam>
        /// <returns>排序后的数据集</returns>
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source,
            string propertyName,
            SortDirection sortDirection)
        {
            source.NotNull("source");
            propertyName.NotNullOrEmpty("propertyName");

            return CollectionPropertySorter<T>.OrderBy(source, propertyName, sortDirection);
        }

        /// <summary>
        /// 把<see cref="IQueryable{T}"/>集合按指定字段排序条件进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="orderCondition">列表字段排序条件</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, OrderCondition orderCondition)
        {
            source.NotNull("source");
            orderCondition.NotNull("orderCondition");

            return source.OrderBy(orderCondition.SortField, orderCondition.SortDirection);
        }

        /// <summary>
        /// 把<see cref="IQueryable{T}"/>集合按指定字段排序条件进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="orderCondition">列表字段排序条件</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, OrderCondition<T> orderCondition)
        {
            source.NotNull("source");
            orderCondition.NotNull("orderCondition");
            return source.OrderBy(orderCondition.SortField, orderCondition.SortDirection);
        }

        /// <summary>
        /// 把<see cref="IOrderedQueryable{T}"/>集合继续按指定字段排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="propertyName">排序属性名</param>
        /// <param name="sortDirection">排序方向</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source,
            string propertyName,
           SortDirection sortDirection = SortDirection.Ascending)
        {
            source.NotNull("source");
            propertyName.NotNull("propertyName");

            return CollectionPropertySorter<T>.ThenBy(source, propertyName, sortDirection);
        }

        /// <summary>
        /// 把<see cref="IOrderedQueryable{T}"/>集合继续指定字段排序方式进行排序
        /// </summary>
        /// <typeparam name="T">动态类型</typeparam>
        /// <param name="source">要排序的数据集</param>
        /// <param name="orderCondition">列表字段排序条件</param>
        /// <returns></returns>
        public static IOrderedQueryable<T> ThenBy<T>(this IOrderedQueryable<T> source, OrderCondition orderCondition)
        {
            source.NotNull("source");
            orderCondition.NotNull("sortCondition");

            return source.ThenBy(orderCondition.SortField, orderCondition.SortDirection);
        }
        #endregion
    }
}

